package club.zhilin.guli.service.edu.controller.backend;


import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.edu.domain.Chapter;
import club.zhilin.guli.service.edu.domain.vo.ChapterVO;
import club.zhilin.guli.service.edu.service.IChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zhilin
 * @since 2022-01-11 15:29:01
 */
@Api(tags = "章节管理")
@RestController
@RequestMapping("/admin/edu/chapter")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ChapterController {

    private final IChapterService iChapterService;

    @ApiOperation("新增章节")
    @PostMapping("/chapter")
    public CommonResult<String> save(@ApiParam("章节对象") @RequestBody Chapter chapter) {
        boolean result = iChapterService.save(chapter);
        return result ? CommonResult.success("保存成功") : CommonResult.failed("保存失败");
    }

    @ApiOperation("根据章节id查询章节")
    @GetMapping("/chapter/{id}")
    public CommonResult<Chapter> getChapterById(@ApiParam("章节id") @PathVariable String id) {
        Chapter chapter = iChapterService.getById(id);
        return chapter != null ? CommonResult.success(chapter) : CommonResult.failed("数据不存在");
    }

    @ApiOperation("更新章节")
    @PutMapping("/chapter")
    public CommonResult<String> updateChapter(@RequestBody Chapter chapter) {
        boolean result = iChapterService.updateById(chapter);
        return result ? CommonResult.success("修改成功") : CommonResult.failed("修改失败");
    }

    @ApiOperation("根据章节id删除章节")
    @DeleteMapping("/chapter/{id}")
    public CommonResult<String> deleteChapter(@PathVariable String id) {
        iChapterService.removeChapterById(id);
        return CommonResult.success();
    }

    @ApiOperation("嵌套章节列表")
    @GetMapping("/nestedList/{courseId}")
    public CommonResult<List<ChapterVO>> nestedList(@PathVariable String courseId) {
        return CommonResult.success(iChapterService.nestedList(courseId));
    }
}

